package com.grape.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grape.constants.Constants;
import com.grape.utils.StringUtils;
import com.grape.utils.ThreadLocalUtil;
import com.grape.vo.BaseVo;
import com.grape.vo.DisableVO;
import com.grape.vo.EnableVO;
import com.grape.wrapper.BaseWrapper;
import com.grape.domain.MemberGradePrice;
import com.grape.mapper.MemberGradePriceMapper;
import com.grape.service.IMemberGradePriceService;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * 会员等级价格表 服务实现类
 *
 * @author      putao
 * @email       2628279194@qq.com
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MemberGradePriceServiceImpl extends ServiceImpl<MemberGradePriceMapper, MemberGradePrice> implements IMemberGradePriceService {

    private final MemberGradePriceMapper memberGradePriceMapper;

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(MemberGradePriceServiceImpl.class);

    /**
     * 分页
     * @param baseVo    统一分页对象
     * @return          会员等级价格表集合
     */
    @Override
    public List<MemberGradePrice> listData(BaseVo baseVo) {
        QueryWrapper<MemberGradePrice> wrapper = new BaseWrapper<MemberGradePrice>().init();
        wrapper.like(Strings.isNotBlank(baseVo.getSearch()), Constants.NAME, baseVo.getSearch())
                .eq(StringUtils.isNotBlank(baseVo.getColumnName()) && StringUtils.isNotBlank(baseVo.getSelect()), baseVo.getColumnName(), baseVo.getSelect());
        orderByModifyTimeDesc(wrapper);
        return this.list(wrapper);
    }

    /**
     * 自定义分页
     * @param   baseVo      统一分页对象
     */
    @Override
    public void pageListCustom(BaseVo baseVo) {
        String userId = ThreadLocalUtil.getUserId();
        QueryWrapper<MemberGradePrice> qw = new QueryWrapper<MemberGradePrice>().eq(Constants.USER_ID, userId)
                .orderByDesc(Constants.CREATE_TIME);
        this.memberGradePriceMapper.selectList(qw);
    }

    /**
     * 修改
     * @param memberGradePrice 会员等级价格表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void updateData(MemberGradePrice memberGradePrice) {
        this.baseUpdate(memberGradePrice);
    }

    /**
     * 新增
     * @param       memberGradePrice 会员等级价格表对象
     * @return      会员等级价格表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public MemberGradePrice insertData(MemberGradePrice memberGradePrice) {
        return this.baseInsert(memberGradePrice);
    }

    /**
     * 删除
     * @param id    单据id
     * @return      删除条数
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public int deleteData(String id) {
        return this.baseDelete(id);
    }

    /**
     * 根据id查询详情
     * @param       id  单据id
     * @return      会员等级价格表对象
     */
    @Override
    public MemberGradePrice detail(String id) {
        return this.getByIdForDelFlag(id);
    }

    /**
     * 更新会员等级价格状态
     * @param id 会员等级价格ID
     * @param newStatus 新状态
     * @param currentStatus 当前状态
     * @param errorMessage 错误信息
     */
    private void updateStatus(String id, String newStatus, String currentStatus, String errorMessage) {
        MemberGradePrice price = this.getByIdForDelFlag(id);
        if (currentStatus.equals(price.getStatus())) {
            throw new RuntimeException(errorMessage);
        }
        price.setStatus(newStatus);
        price.updateInit();
        this.updateById(price);
    }

    /**
     * 禁用会员等级价格
     * @param disableVO 禁用参数
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void disable(DisableVO disableVO) {
        updateStatus(disableVO.getId(), Constants.ZERO_STR, Constants.ZERO_STR, "该会员等级价格已禁用");
    }

    /**
     * 启用会员等级价格
     * @param enableVO 启用参数
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void enable(EnableVO enableVO) {
        updateStatus(enableVO.getId(), Constants.ONE_STR, Constants.ONE_STR, "该会员等级价格已启用");
    }
}
